/*
 * commonFunc.java
 *
 * Created on 2007/08/06, 19:58
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package xsd;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author verifsp7
 */
public class comFunc {
    
    /** Creates a new instance of commonFunc */
    public comFunc() {
    }
    public static String getEncodingInFile(String filepath) 
        throws FileNotFoundException, IOException{
        if(filepath==null) throw new FileNotFoundException();
        String ret="UTF-8";
        BufferedReader br=new BufferedReader(new FileReader(filepath));
        String line;
        String hitline="";
        while((line=br.readLine())!=null){
            hitline += line;
            if(!line.matches(".*\\?>.*")) continue;
            Pattern p=Pattern.compile("<\\?xml.*encoding\\s*=\\s*\"(.+?)\".*\\?>");
            Matcher m=p.matcher(hitline);
            if(m.find() && m.groupCount()>0){
                ret=m.group(1).trim();
            }
        }
        br.close();
        return(ret);        
    }
    public static String encodingMap(String orig){
        String ret="UTF-8";
        String a=orig.toUpperCase();
        if(a.matches("^(EUC-JP|JA_JP\\.EUCJP|EUC|EUCJP)(-.*)??$")) ret="EUC-JP";
        if(a.matches("^(SHIFT|SHIFT-JIS|SJIS)$")) ret="Shift-JIS";
        return(ret);
    }
    
    // ***** CUSTOMIZED CODE *****
    // for xmlSvrfSyntax
}
